﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows;


namespace cc
{
    public partial class Form1 : Form
    {
        Aithmetic ar = new Aithmetic();
        Trigonometric tr = new Trigonometric();
        int key = 0; // 0 - продолжать ввод, 1 - начинать ввод с первой цифры
        string operant = "";
        int flag = 0;
        


        public Form1()
        {
            InitializeComponent();
        }

        public void ravno(string a)
        {
            if ((a == "sin") || (a == "cos") || (a == "tg"))
            {
             
                tr.operant1 = ar.operant1; 
                tr.t_equally(a);
 
            }
            else ar.a_equally(a);
        }

        public void forOne(string a)
        {
            if ((operant != "") && (key != 1))
            {
                equal_Click(null, null);
              //  ravno(a);
                if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                {
                    textBox.Text = "ошибка";
                    operant = "";
                }
                else
                {
                    textBox.Text = Convert.ToString(ar.rezult);
                    operant = "";
                }
               
            }
            else
            {
                operant = a;
                ar.operant1 = Convert.ToDouble(textBox.Text);
              //  ar.operant1 = ar.rezult;
                ravno(a);
               // equal_Click(null, null);
                  
                if (((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))||((tr.rezult == 0) && (tr.operant1 == -2) && (tr.operant2 == -1)))
                {
                    textBox.Text = "ошибка";                                     
                }

                else

                if (textBox.Text != "")
                {
                    if ((a == "sin") || (a == "cos") || (a == "tg") )
                    textBox.Text = Convert.ToString(tr.rezult);
                    else
                    textBox.Text = Convert.ToString(ar.rezult);

                }
                operant = "";
                key = 1;
            }
            flag = 0;

        }

        public void forTwo(string a)
        {
            if ((operant != "") && (key != 1))
            {
                equal_Click(null, null);
                if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                {
                    textBox.Text = "ошибка";
                    operant = "";
                }
                else
                {
                    ar.operant1 = ar.rezult;
                    operant = a;
                }
            }

            else
            {
                if ((textBox.Text != "ошибка") && (textBox.Text != ""))
                {
                    ar.operant1 = Convert.ToDouble(textBox.Text);
                    operant = a;
                }
                key = 1;
            }
            flag = 0;
        }

        private void equal_Click(object sender, EventArgs e)//=
        {
            if ((operant != "") && (key == 0))
            {
                ar.operant2 = Convert.ToDouble(textBox.Text);
                //if (operant != "")                            !!!!!!!!!!!!!!!!!
                    ravno(operant);
                if ((ar.rezult == 0) && (ar.operant1 == -2) && (ar.operant2 == -1))
                {
                    textBox.Text = "ошибка";
                    operant = "";
                }
                else textBox.Text = Convert.ToString(ar.rezult);
                key = 1;
                operant = "";
                
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            flag = 0;
           // textBox.Text = "0";
           

        }

        private void display_TextChanged(object sender, EventArgs e)
        {

        }

        private void button_7_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(7);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(7);

        }

        private void button_1_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(1);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(1);

        }

        private void button_2_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(2);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(2);

        }

        private void button_3_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(3);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(3);

        }

        private void button_4_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(4);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(4);

        }

        private void button_5_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(5);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(5);
        }

        private void button_6_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(6);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(6);
        }

        private void button_8_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(8);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(8);
        }

        private void button_9_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(9);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(9);
        }

        private void button_0_Click(object sender, EventArgs e)
        {
            if (key == 1)
            {
                textBox.Text = Convert.ToString(0);
                key = 0;
            }
            else textBox.Text = textBox.Text + Convert.ToString(0);
        }

        private void divide_Click(object sender, EventArgs e)
        {
            forTwo("/");
        }

        private void multiplied_Click(object sender, EventArgs e)
        {
            forTwo("*");
        }

        private void minus_Click(object sender, EventArgs e)
        {
            forTwo("-");
        }

        private void plus_Click(object sender, EventArgs e)
        {
            forTwo("+");
        }

        private void cos_Click(object sender, EventArgs e)
        {
            forOne("cos");
        }

        private void tg_Click(object sender, EventArgs e)
        {
            forOne("tg");
        }

        private void sin_Click(object sender, EventArgs e)
        {
            forOne("sin");
        }

        private void square_Click(object sender, EventArgs e)
        {
            forOne("sqrt");
        }

        private void pow_Click(object sender, EventArgs e)
        {
            forTwo("^");
        }

        private void comma_Click(object sender, EventArgs e)
        {
            if ((textBox.Text != "ошибка") && (textBox.Text != "") && (key == 0) && (flag == 0))
            {
                textBox.Text = textBox.Text + ",";
                flag = 1;
            }
        }

        private void clear_Click(object sender, EventArgs e)
        {
            key = 0;
            flag = 0;
            operant = "";
            ar.operant1 = 0;
            ar.operant2 = 0;
            tr.operant1 = 0;
            tr.operant2 = 0;
            tr.rezult = 0;
            ar.rezult = 0;
            textBox.Text = "";
        }

        private void вычислениеАлгебраическихОперацийToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Вычисление алгебраических операций.txt");
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void выToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Вычисление тригонометрических функций.txt");
        }

        private void sign_Click(object sender, EventArgs e)
        {
            if (textBox.Text != "")
            textBox.Text = Convert.ToString(Convert.ToDouble(textBox.Text) * (-1));
        }

        private void but_Click(object sender, EventArgs e)
        {
           Form1.ActiveForm.Width = 295;

            panel.Visible = true;
        }

        private void but2_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Width = 223;
            panel.Visible = false;
        }

        private void обычныйToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Width = 223;
            panel.Visible = false;
        }

        private void инженерныйToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1.ActiveForm.Width = 295;

            panel.Visible = true;
        }

       
    }
}
